package com.example.myapplication.ui.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
import com.example.myapplication.data.RecordRepository;
import com.example.myapplication.databinding.FragmentSettingsBinding;

public class SettingsFragment extends Fragment {
    private FragmentSettingsBinding binding;
    private SharedPreferences sharedPreferences;
    private RecordRepository repository;
    private static final String PREF_NAME = "EasyAccountPrefs";
    private static final String KEY_BUDGET = "monthly_budget";

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                            ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentSettingsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        repository = new RecordRepository(requireActivity().getApplication());
        
        // 初始化SharedPreferences
        sharedPreferences = requireContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

        // 加载保存的预算
        String savedBudget = sharedPreferences.getString(KEY_BUDGET, "");
        binding.budgetEdit.setText(savedBudget);

        // 设置保存按钮点击事件
        binding.saveButton.setOnClickListener(v -> saveBudget());

        // 添加清除数据按钮的点击事件
        binding.clearDataButton.setOnClickListener(v -> showClearDataDialog());

        return root;
    }

    private void saveBudget() {
        String budgetStr = binding.budgetEdit.getText().toString().trim();
        
        // 验证输入
        if (budgetStr.isEmpty()) {
            showToast(R.string.error_empty);
            return;
        }

        try {
            double budget = Double.parseDouble(budgetStr);
            if (budget <= 0) {
                showToast(R.string.error_negative);
                return;
            }

            // 保存预算
            sharedPreferences.edit()
                    .putString(KEY_BUDGET, budgetStr)
                    .apply();

            showToast(R.string.save_success);
        } catch (NumberFormatException e) {
            showToast(R.string.error_invalid);
        }
    }

    private void showClearDataDialog() {
        new AlertDialog.Builder(requireContext())
                .setTitle("清除数据")
                .setMessage("确定要清除所有记录吗？此操作不可恢复！")
                .setPositiveButton("确定", (dialog, which) -> {
                    repository.deleteAllRecords();
                    Toast.makeText(requireContext(), "所有记录已清除", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void showToast(int messageResId) {
        Toast.makeText(requireContext(), messageResId, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
} 